package com.chonglie.tools;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.chonglie.R;

public abstract class ImageUtils {

	public static List<Integer> getImageList(String prefix) {
		List<Integer> imageList = new ArrayList<Integer>();

		Field[] fields = R.drawable.class.getDeclaredFields();

		for (Field field : fields) {
			if (isLoadIt(field.getName(), prefix)) {
				try {
					imageList.add(field.getInt(R.drawable.class));
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			}
		}

		return imageList;
	}

	private static boolean isLoadIt(String name, String prefix) {
		if (prefix != null) {
			return name.startsWith(prefix);
		} else {
			return true;
		}
	}

	public static List<int[]> getImageSizes(Context context,
			List<Integer> imageList) {
		List<int[]> imageSizeList = new ArrayList<int[]>();
		for (Integer index : imageList) {
			Bitmap img = BitmapFactory.decodeResource(context.getResources(),
					index);
			int[] size = new int[2];
			size[0] = img.getWidth();
			size[1] = img.getHeight();
			imageSizeList.add(size);
		}
		return imageSizeList;
	}

	public static List<Bitmap> getBitmaps(Context context,
			List<Integer> imageList) {
		List<Bitmap> result = new ArrayList<Bitmap>();
		for (Integer index : imageList) {
			Bitmap img = BitmapFactory.decodeResource(context.getResources(),
					index);
			result.add(img);

		}
		return result;
	}

}
